package com.usian.controller;

import com.usian.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping("findByConsumer")
    public User findById(){
        ServiceInstance provider = loadBalancerClient.choose("provider");
        String url="http://"+provider.getHost()+":"+provider.getPort()+"/findById/14";

        User user = restTemplate.getForObject(url, User.class);
        return user;
    }
}
